<?php
namespace app\api\controller;
use app\common\controller\Base;

/**
 * 广告位
 */
class AdPub extends Base{

   
    //广告价位列表
    public function adList(){
        $list = db('ad_price')->field('foreign_key,price,id')->select();
        foreach($list as $key => $val){
            switch($val['foreign_key']){
                case 1: $list[$key]['position'] = '开屏广告';break;
                case 2: $list[$key]['position'] = '每日快报广告位';break;
                case 3: $list[$key]['position'] = '附近帮主广告位';break;
                case 4: $list[$key]['position'] = '附近圈广告位';break;
            }
        }
        $this->apiReturn(1,'列表',$list);
    }


    //订单详情
    public function orderInfo(){
        if(!isset($this->post['id']) || intval($this->post['id']) <= 0){
            $this->apiReturn(0,'参数id丢失');
        }
        $info = db('ad_buy')->field('position_id,hangye,price,buy_num,use_num,order_no,status,FROM_UNIXTIME(pay_time,"%Y-%m-%d %H:%i") as pay_time,title,image,content,video,FROM_UNIXTIME(add_time,"%Y-%m-%d %H:%i") as add_time')->find(intval($this->post['id']));
        if(is_null($info)){
            $this->apiReturn(0,'订单不存在或已被删除');
        } 
        if($info['status'] != 0 && $info['use_num'] >= $info['buy_num']){
            $info['status'] = 2;
        }
        $info['position'] = db('ad_position')->where('id',$info['position_id'])->value('position');
        $info['hangye'] = $info['hangye'] == 0 ? '' : db('hangye')->where('id',$info['hangye'])->value('name');
        $info['content'] = empty($info['content']) ? '' : $info['content'] ;
        $info['video'] = empty($info['video']) ? '' : $info['video'] ;
        $info['pay_time'] = $info['status'] == 0 ? '' : $info['pay_time'];
        $info['image'] = empty($info['image']) ? [] : explode(',',$info['image']);
        $this->apiReturn(1,'详情',$info);
    }

    //广告详情
    public function adInfo($id = 0){
        $info = db('ad_buy')->alias('a')->join(['bz_user' => 'u'],'u.id = a.user_id')->where('a.id',$id)->field('title,content,video,image,nick_name,photo_path,kaopu,service_count,zhiye,user_id,tag,sex')->find();
        $info['content'] = empty($info['content']) ? '' : $info['content'] ;
        $info['video'] = empty($info['video']) ? '' : $info['video'] ;
        $info['image'] = empty($info['image']) ? [] : explode(',',$info['image']);
        $info['zhiye'] = $info['zhiye'] == 0 ? '' : db('zhiye')->where('id',$info['zhiye'])->value('value');
        $info['tag'] = $info['zhiye'] == '' ? [] : explode(',',$info['tag']);
        $this->apiReturn(1,'详情',$info);
    }

    //广告位展示
    public function adShow($type = 0){
        $model = db('ad_buy')->alias('b')->join(['bz_user' => 'u'],'u.id = b.user_id','left')->where('position_id',$type)->where('b.status',1)->where('use_num','exp','< buy_num')->where('issue_time','elt',time())->order('pay_time asc')->field('b.id,title,image,content,video,nick_name,photo_path,FROM_UNIXTIME(pay_time,"%Y-%m-%d") as add_time');
        if($type != 2){
            $model->limit(1);
        }
        $list = $model->select();
        foreach($list as $key => $val){
            $list[$key]['content'] = empty($val['content']) ? '' : $val['content'] ;
            $list[$key]['video'] = empty($val['video']) ? '' : $val['video'] ;
            $list[$key]['image'] = empty($val['image']) ? [] : explode(',',$val['image']);

            db('ad_buy')->where('id',$val['id'])->setInc('use_num');
        }

        $this->apiReturn(1,'',$list);
    }
}
